/**
 * 
 */
package com.shine.icis.blo.imp;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.shine.cotrun.exp.Export;
import com.shine.cotrun.exp.ExportConf;
import com.shine.cotrun.exp.ExportFactory;
import com.shine.cotrun.web.comm.ListPage;
import com.shine.icis.blo.IETLLogService;
import com.shine.icis.config.SysCodeDictLoader;
import com.shine.icis.dao.IETLLogDAO;
import com.shine.icis.dao.param.ETLRunLogQueryParameters;
import com.shine.icis.dto.DictOption;
import com.shine.icis.hbean.ETLRunLog;
import com.shine.icis.hbean.ProgExeLog;

/**
 * @author zsy
 *
 */
public class ETLLogService implements IETLLogService {
	
	private IETLLogDAO etlLogDAO;

	public void setEtlLogDAO(IETLLogDAO etlLogDAO) {
		this.etlLogDAO = etlLogDAO;
	}

	@Override
	public List<DictOption> getEtlProcOptions() {
		return etlLogDAO.findEtlProcOptions();
	}

	@Override
	public List<DictOption> getPrepLevelOptions() {
		return etlLogDAO.findPrepLevelOptions();
	}
	
	@Override
	public List<DictOption> getBizSystemOptions() {
		return etlLogDAO.findBizSystemOptions();
	}

	@Override
	public ListPage<ETLRunLog> queryETLRunLogPage(ETLRunLogQueryParameters qp) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		return etlLogDAO.findETLRunLogPage(qp);
	}
	/**
	 * 运行日志导出Excel
	 * @author zhangxr 2010-12-27
	 * */
	@Override
	public String csExpDataRunLog(ETLRunLogQueryParameters qp, String dir, String expType) {
		if (qp == null) {
			throw new IllegalArgumentException();
		}
		//初始化配置信息
		ExportConf cfg = new ExportConf();
		cfg.setExpToDir(dir);
		cfg.setExpFileName("运行日志");
		cfg.addField("级别", "prepLevelText", String.class, 10);
		cfg.addField("操作日期", "formatOperDate", String.class, 10);
		cfg.addField("操作时间", "formatOperTime", String.class, 8);
		cfg.addField("类别", "etlProcText", String.class, 20);
		cfg.addField("状态", "runStatusText", String.class, 10);
		cfg.addField("信息情况", "resultMsg", String.class, 256);

		Export<ETLRunLog> exp = ExportFactory.createInstance(expType, cfg);
		try {
			exp.init();//初始化工作
			
			//添加额处查询条件
			qp.setPageNo(1);
			qp.setPageSize(10000);
			
			ListPage<ETLRunLog> page = this.etlLogDAO.findETLRunLogPage(qp);//查第一页
			//第一页写到文件
			exp.export(page.getDataList());
			if (page.getTotalPageCount() > 1) {
				for (int i = 2; i <= page.getTotalPageCount(); i++) {
					qp.setPageNo(i);
					page = this.etlLogDAO.findETLRunLogPage(qp);//查第i页
					//第i页写到文件
					exp.export(page.getDataList());
				}
			}
		} catch(Exception e) {
			e.printStackTrace();
		} finally {
			//结束写入
			exp.end();
		}
		return exp.getFileName();//返回文件名（含扩展名）
	}
		
	@Override
	public void ntxAddETLRunLog(int etlProcID, int prepLevelID, char runStatus, String resultMsg) {
//		ETLRunLog erl = new ETLRunLog();
//		erl.setEtlProcID(etlProcID);
//		erl.setPrepLevelID(prepLevelID);
//		erl.setRunStatus(runStatus);
//		erl.setResultMsg(resultMsg);
//		
////		Date now = new Date();
//		Date now = etlLogDAO.findDatabaseDate();
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//		erl.setOperDate(Integer.parseInt(sdf.format(now)));
//		
//		sdf = new SimpleDateFormat("HHmmss");
//		erl.setOperTime(Integer.parseInt(sdf.format(now)));
//		etlLogDAO.save(erl);
		//改为调用过程写LOG，这样与TASKSERVER行为一致
		String value = SysCodeDictLoader.getInstance().getSubmitConfValue("ETL_LOG_LEVEL");
		if (prepLevelID <= Integer.parseInt(value)) {
			etlLogDAO.saveRunLog(etlProcID, prepLevelID, runStatus, resultMsg);
		}
	}
	
	@Override
	public void ntxAddProgExeLog(char etlType, Integer etlProcID, char runStatus) {
		ProgExeLog pel = new ProgExeLog();
		pel.setEtlType(etlType);
		pel.setEtlProcID(etlProcID);
		pel.setRunStatus(runStatus);
		
//		Date now = new Date();
		Date now = etlLogDAO.findDatabaseDate();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		pel.setOperDate(Integer.parseInt(sdf.format(now)));
		
		sdf = new SimpleDateFormat("HHmmss");
		pel.setOperTime(Integer.parseInt(sdf.format(now)));
		etlLogDAO.save(pel);
	}
	
	@Override
	public void txClearHistData() {
		//删除日志
		String value = SysCodeDictLoader.getInstance().getSubmitConfValue("LOG_RETAIN_DAYS");
		if (value == null) {
			return;
		}
		int months = Integer.parseInt(value);
		if (months < 1) {
			return;
		}
		Calendar now = Calendar.getInstance();
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) - months);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		int date = Integer.parseInt((sdf.format(now.getTime())));//要删除这天前的数据
		etlLogDAO.callProc("{call EB_METADB.P_LOG_DEL(?,?,?,?,?,?,?,?)}", date);
		
		//删除备份库数据
		value = SysCodeDictLoader.getInstance().getSubmitConfValue("DATA_RETAIN_DAYS");
		if (value == null) {
			return;
		}
		months = Integer.parseInt(value);
		if (months < 1) {
			return;
		}
		now = Calendar.getInstance();
		now.set(Calendar.MONTH, now.get(Calendar.MONTH) - months);
		sdf = new SimpleDateFormat("yyyyMMdd");
		date = Integer.parseInt((sdf.format(now.getTime())));//要删除这天前的数据
		etlLogDAO.callProc("{call EB_HIST.P_ARC_HIST_DEL(?,?,?,?,?,?,?,?)}", date);//删除历史库数据
		etlLogDAO.callProc("{call EB_ANALYSE.P_ICIS_ANALYSE_DEL(?,?,?,?,?,?,?,?)}", date);//删除应用层备份数据
	}
}
